package lt.marsrutai.mobile.android.util;

import android.os.Parcel;

/**
 * Utility methods for handling parcels
 * 
 * @author Justas
 */
public class ParcelUtil {

    /**
     * Writes a value to the parcel. Value may be <code>null</code>. Value
     * should be read from parcel using {@link #readDouble(Parcel)}.
     * 
     * @param parcel
     * @param value
     */
    public static void writeDouble(Parcel parcel, Double value) {
        if (value == null) {
            parcel.writeInt(0);
        }
        else {
            parcel.writeInt(1);
            parcel.writeDouble(value.doubleValue());
        }
    }

    /**
     * Reads a value from the parcel. Value may be <code>null</code>. Only
     * values written to the parcel with {@link #writeDouble(Parcel, Double)}
     * should be read by calling this method.
     * 
     * @param parcel
     * @return
     */
    public static Double readDouble(Parcel parcel) {
        Double value = null;

        if (parcel.readInt() == 1) {
            value = parcel.readDouble();
        }

        return value;
    }

    /**
     * Writes a value to the parcel. Value may be <code>null</code>. Value
     * should be read from parcel using {@link #readInteger(Parcel)}.
     * 
     * @param parcel
     * @param value
     */
    public static void writeInteger(Parcel parcel, Integer value) {
        if (value == null) {
            parcel.writeInt(0);
        }
        else {
            parcel.writeInt(1);
            parcel.writeInt(value);
        }
    }

    /**
     * Reads a value from the parcel. Value may be <code>null</code>. Only
     * values written to the parcel with {@link #writeInteger(Parcel, Integer)}
     * should be read by calling this method.
     * 
     * @param parcel
     * @return
     */
    public static Integer readInteger(Parcel parcel) {
        Integer value = null;

        if (parcel.readInt() == 1) {
            value = parcel.readInt();
        }

        return value;
    }

    /**
     * Writes a value to the parcel. Value may be <code>null</code>. Value
     * should be read from parcel using {@link #readLong(Parcel)}.
     * 
     * @param parcel
     * @param value
     */
    public static void writeLong(Parcel parcel, Long value) {
        if (value == null) {
            parcel.writeInt(0);
        }
        else {
            parcel.writeInt(1);
            parcel.writeLong(value);
        }
    }

    /**
     * Reads a value from the parcel. Value may be <code>null</code>. Only
     * values written to the parcel with {@link #writeLong(Parcel, Long)} should
     * be read by calling this method.
     * 
     * @param parcel
     * @return
     */
    public static Long readLong(Parcel parcel) {
        Long value = null;

        if (parcel.readInt() == 1) {
            value = parcel.readLong();
        }

        return value;
    }

    public static boolean readBoolean(Parcel parcel) {
        return parcel.readByte() == 1;
    }

    public static void writeBoolean(Parcel parcel, boolean value) {
        parcel.writeByte((byte) (value ? 1 : 0));
    }

    public static void writeEnum(Parcel parcel, Enum<?> value) {
        if (value == null) {
            parcel.writeInt(0);
        }
        else {
            parcel.writeInt(1);
            parcel.writeString(value.name());
        }
    }

    public static <T extends Enum<T>> T readEnum(Parcel parcel, Class<T> enumType) {
        T value = null;

        if (parcel.readInt() == 1) {
            value = Enum.valueOf(enumType, parcel.readString());
        }

        return value;
    }
}
